﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Accountant.Primitives;

namespace Accountant.DataModel
{
  partial class Transaction
  {
    private static readonly TimeSpan Epsilon = TimeSpan.FromSeconds(1.001);
    private static DateTime _lastTimestamp;
    /// <summary>Gets new timestamp that is quite close to now, 
    ///   yet distinct from the previous one. </summary>
    /// <returns>Unique timestamp.</returns>
    /// <remarks>Use to prevent unconscious timestamp collisions</remarks>
    public static DateTime GenerateTimestamp(DateTime? baseDate = null)
    {
      var n = baseDate ?? DateTime.UtcNow;
      if (n - _lastTimestamp < Epsilon)
      {
        _lastTimestamp += Epsilon;
      }
      else
      {
        _lastTimestamp = n;
      }
      return _lastTimestamp;
    }

    public override string ToString()
    {
      return string.Format("({0}) ----{1}---> ({2})", Debit.Name, MoneyString, Credit.Name);
    }
    
    [NotMapped]
    public string MoneyString { get { return Money.ToString(); } }

    [NotMapped]
    public Money Money { get { return new Money(Currency, Amount); } }
  }
}